//
//  ScrollView.m
//  FlyGift
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ScrollView.h"

@implementation ScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.myScrollView = [[CycleScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth / 2.5) animationDuration:3.0];
        self.myScrollView.scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_myScrollView];
        
        self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(kScreenWidth - 100, _myScrollView.frame.size.height - 20, 100, 20)];
        [_myScrollView addSubview:_pageControl];
    }
    return self;
}

-(void)setImageArray:(NSMutableArray *)imageArray
{
    NSMutableArray * array = [NSMutableArray array];
    for (int i = 0; i < imageArray.count; i++ ) {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth / 2.5)];
        imageView.userInteractionEnabled = YES;
        ScrollModel * model = imageArray[i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.image_url] placeholderImage:[UIImage imageNamed:@"placeHolder@2x.jpg"]];
        [array addObject:imageView];
    }
    
    self.pageControl.numberOfPages = array.count;
    
    __block typeof(self) blockSelf = self;
    self.myScrollView.fetchContentViewAtIndex = ^UIView * (NSInteger pageIndex){
        if (pageIndex == 0) {
            blockSelf.pageControl.currentPage = array.count - 1;
        }
        else
        {
            blockSelf.pageControl.currentPage = pageIndex - 1;
        }
        return array[pageIndex];
    };
    
    
    self.myScrollView.totalPagesCount = ^NSInteger(){
        return imageArray.count;
    };
    
}


@end
